Web Application

The WebAppTestCase deploys an OSGi Web Application Bundle (WAB). Similar to HTTP Service Example it registers a servlet and resources with the WebApp container. This is done through a standard web.xml descriptor.

<web-app xmlns="http://java.sun.com/xml/ns/javaee" ... version="2.5">

  <display-name>WebApp Sample</display-name>

  <servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>org.jboss.test.osgi.example.webapp.bundle.EndpointServlet</servlet-class>
    <init-param>
      <param-name>initProp</param-name>
      <param-value>SomeValue</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/servlet</url-pattern>
  </servlet-mapping>

</web-app>

The associated OSGi manifest looks like this.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: example-webapp
Bundle-ClassPath: .,WEB-INF/classes
Web-ContextPath: example-webapp
Import-Package: javax.servlet,javax.servlet.http,...

The test verifies that we can access the servlet and some resources.

public void testServletAccess() throws Exception
{
   // Provide WebApp support
   WebAppSupport.provideWebappSupport(context, bundle);
        
   // Start the test bundle
   bundle.start();
        
   String line = getHttpResponse("/example-webapp/servlet?test=plain", 5000);
   assertEquals("Hello from Servlet", line);
}

OSGi Web Application support in AS7

This test uses the OSGi Repository functionality to provision the runtime with the required support functionality like this

WebAppSupport.provideWebappSupport(context, bundle);

To enable OSGi Web Application support in AS7 you would configure these capabilities

<capability name="org.ops4j.pax.web:pax-web-jetty-bundle:1.1.2"/>
<capability name="org.ops4j.pax.web:pax-web-jsp:1.1.2"/>
<capability name="org.ops4j.pax.web:pax-web-extender-war:1.1.2"/>